<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('Controller', 'Controller');
App::uses('Security', 'Utility');
/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		app.Controller
 * @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {

	var $usuarioSessao;
	var $components = array(
			'Datas',
			'Session',
			'Auth'=>array(	
					'authenticate'=>array(
							'Form' => array(
									'userModel' => 'Login',
									'fields'=>array(
											'username'=>'login',
											'password'=>'senha'
									)
							)
					),
					'loginAction' => array(
							'plugin'=>false,
							'controller' => 'login',
							'action' => 'index',
					),
					'authError' => 'Você deve fazer login antes de continuar',
			)
	);
	
	public $helpers = array('BootstrapCake.Bootstrap', 'Session');
	
	public function beforeFilter(){
		Security::setHash('sha256');
		parent::beforeFilter();
		//$this->set('title_for_layout','nhaga');
		//$this->Auth->allow();
		$this->loadModel('Idioma');
		$this->carrega_configs();
		$this->set('sys_name', $this->Session->read('config.nome_evento'));
		$this->layout = 'scige_bs';
		$this->carregaSessao();
		$this->set('usuario_sessao', $this->usuarioSessao);
		$this->paginate = array('maxLimit' => 10);
		$this->Session->write('PaymentPlugin.adminFunction', 'isAdminGeral');
		$this->set('idiomas_select', $this->Idioma->find('list', array('conditions' => array('ativo =' => 1))));
//		pr($this->usuarioSessao);
		//pr(Configure::read('Config.language'));
		//pr($_SESSION);
// 		pr($_SERVER);
// 		pr(Configure::read('Config.language'));
		if ($this->Session->check('Config.language')) {
			Configure::write('Config.language', $this->Session->read('Config.language'));
		}
		else{
			if(!isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
				Configure::write('Config.language', 'pt');
			else{
				$lang_server = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
				if(strlen($lang_server) > 2)
					$lang_server = substr($lang_server, 0, 2); 
				Configure::write('Config.language', $lang_server);
			}
		}
	}
	
	
	public function carregaSessao(){
		$this->usuarioSessao = $this->Auth->user();
		$this->tenhoInscricao();
	}
	
	public function tenhoInscricao(){
		if(isset($this->usuarioSessao['id'])){
			if(!$this->Session->check('Inscricao.inscricao_feita')){
				$this->recarregaInscricao();
			}
			$this->usuarioSessao['Inscricao']['inscricao_feita'] = $this->Session->read('Inscricao.inscricao_feita');
			$this->usuarioSessao['Inscricao']['inscricao_confirmada'] = $this->Session->read('Inscricao.inscricao_confirmada');
		}
	}
	
	public function recarregaInscricao(){
		$this->loadModel('Pagamento');
		$info_pagamento = $this->Pagamento->findByUsuarioId($this->usuarioSessao['Usuario']['id']);
		if($info_pagamento){
			$this->Session->write('Inscricao.inscricao_feita', true);
			$this->Session->write('Inscricao.inscricao_confirmada', $info_pagamento['Pagamento']['confirmado']);
		}
		else{
			$this->Session->write('Inscricao.inscricao_feita', false);
			$this->Session->write('Inscricao.inscricao_confirmada', false);
		}
	}
	
	public function isAdmin(){
		if(isset($this->usuarioSessao['tipo']))
			return (int)$this->usuarioSessao['tipo'] === ADMIN || (int)$this->usuarioSessao['tipo'] === ADMIN_LOCAL;
		return false;
	}
	
	public function isAdminGeral(){
		if(isset($this->usuarioSessao['tipo']))
			return (int)$this->usuarioSessao['tipo'] === ADMIN;
		return false;
	}
	
	public function isRevisor(){
		if(isset($this->usuarioSessao['tipo']))
			return (int)$this->usuarioSessao['tipo'] === REVISOR;
		return false;
	}
	
	public function isUsuario(){
		if(isset($this->usuarioSessao['tipo']))
			return (int)$this->usuarioSessao['tipo'] === USUARIO || $this->isRevisor();
		return false;
	}
	
	public function isRecepcionista(){
		if(isset($this->usuarioSessao['tipo']))
			return (int)$this->usuarioSessao['tipo'] === RECEPCIONISTA;
		return false;
	}
	
	public function carrega_configs(){
		$this->loadModel('EventoConfig');
		$carregar = array('nome_evento', 'local_evento', 'tesoureiro', 'email_pagamento', 'token_pagamento', 'pagseguro_evento_code',
				'show_taxas', 'show_datas', 'show_anais', 'outros_sugestoes', 'liberar_certificados_de_apresetacao');
		$configs = $this->EventoConfig->many_values($carregar);
		$this->Session->write('config', $configs);
	}
	
	public function notAuth($msg = null){
		if($msg == null)
			$msg = __('Ação inválida!');
		$this->Session->setFlash($msg, 'flash/bootstrap', array('class'=>'alert alert-success'));
		$this->redirect("/");
	}
}
